<!--
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-02-21 16:57:55
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-03-07 19:45:09
 * @FilePath: \hrManage\src\views\role\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="container">
    <div class="app-container">
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="isShow = true">添加角色</el-button>
      </div>
      <el-table :data="roleData">
        <el-table-column
          prop="name"
          align="center"
          label="角色"
          width="200px"
        >
          <template v-slot="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" label="启用" width="200px">
          <template v-slot="{ row }">
            <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
            <span v-else>
              {{
                row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无"
              }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="" align="center" label="操作">
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <el-button size="mini" type="primary" @click="updateSub(row)">确定</el-button>
              <el-button size="mini" @click="updateCel(row)">取消</el-button>
            </template>
            <template v-else>
              <el-button size="mini" type="text" @click="assignPer(row.id)">分配权限</el-button>
              <el-button size="mini" type="text" @click="edit(row)">编辑</el-button>

              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="del(row.id)"
              > <el-button slot="reference" size="mini" type="text" style="margin-left:10px">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <el-pagination
          layout="prev, pager, next"
          :total="pageData.total"
          :page-size="pageData.pagesize"
          :current-page="pageData.page"
          @current-change="change"
        />
      </el-row>
      <el-dialog title="新增角色" :visible.sync="isShow" @close="btnCancel">
        <el-form ref="roleForm" label-width="120px" :model="roleValue" :rules="roleRules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleValue.name" style="width:300px" size="mini" /></el-form-item>
          <el-form-item label="启用" prop="state">
            <el-switch v-model="roleValue.state" :active-value="1" :inactive-value="0" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleValue.description" style="width:300px" size="mini" type="textarea" resize="none" /></el-form-item>
          <el-form-item>
            <el-row type="flex" justify="center">
              <el-col :span="12">
                <el-button type="primary" @click="btnOk">确定</el-button>
                <el-button @click="btnCancel">取消</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog :visible="roleIsShow" @close="roleCancel">
        <el-tree ref="roleTree" node-key="id" :data="rolePowerList" :props="props" :show-checkbox="true" :check-strictly="true" :default-checked-keys="rolePower" />
        <el-row type="flex" justify="center">
          <el-button type="primary" size="mini" @click="roleSub">提交</el-button>
          <el-button size="mini" @click="roleCancel">取消</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addRole, delRole, getRole, getRoleDetail, updateRole, updateRolePer } from '@/api/role'
import { getPermission } from '@/api/permission'
import { getDepartmentTree } from '@/utils'
export default {
  name: 'Role',
  data() {
    return {
      roleIsShow: false,
      props: {
        label: 'name',
        children: 'children'
      },
      roleData: [],
      pageData: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      isShow: false,
      roleValue: {
        name: '',
        description: '',
        state: ''
      },
      roleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      rolePower: [],
      rolePowerList: [],
      rolePowerId: ''
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    async getRole() {
      const { rows, total } = await getRole(this.pageData)
      this.pageData.total = total
      this.roleData = rows
      this.roleData.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    change(data) {
      this.pageData.page = data
      this.getRole()
    },
    btnOk() {
      this.$refs.roleForm.validate(async(isOk) => {
        if (isOk) {
          await addRole(this.roleValue)
          this.$message.success('添加成功')
          this.getRole()
          this.btnCancel()
        }
      })
    },
    btnCancel() {
      this.$refs.roleForm.resetFields()
      this.isShow = false
    },
    edit(row) {
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    async updateSub(row) {
      if (row.editRow.name && row.editRow.description) {
        await updateRole({ ...row.editRow, id: row.id })
        this.$message.success('修改成功')
        Object.assign(row, {
          ...row.editRow,
          isEdit: false
        })
        this.getRole()
      } else {
        this.$message.error('修改项不能为空')
      }
    },
    updateCel(row) {
      row.isEdit = false
    },
    async del(id) {
      await delRole(id)
      if (this.roleData.length === 1) {
        this.pageData.page--
      }
      this.$message.success('删除成功')
      this.getRole()
    },
    async assignPer(id) {
      this.rolePowerList = getDepartmentTree(await getPermission(), 0)
      this.rolePower = (await getRoleDetail(id)).permIds
      this.rolePowerId = id
      console.log(this.rolePower)
      this.roleIsShow = true
    },
    roleCancel() {
      this.rolePower = []
      this.roleIsShow = false
    },
    async roleSub() {
      await updateRolePer(
        {
          id: this.rolePowerId,
          permIds: this.$refs.roleTree.getCheckedKeys()
        }
      )
      this.$message.success('修改权限成功')
      this.roleCancel()
    }
  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
